package cn.edu.bjfu.it.oyss.covid_19helper.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public final class ProvincesHelper {
    private static final String ID_KEY = "ID";
    private static final String NAME_KEY = "NAME";
    private static final String RISK_KEY = "RISK";
    private static final String IMAGE_RESOURCE_ID = "IMAGE_RESOURCE_ID";
    private static ProvincesHelper singleton;
    public SQLiteOpenHelper DatabaseHelper;
    private ProvincesHelper(Activity activity) {
        DatabaseHelper = new CovidDataBaseHelper(activity);
    }

    public static ProvincesHelper getInstance(Activity activity) {
        if (singleton == null) {
            singleton = new ProvincesHelper(activity);
        }
        return singleton;
    }


    // 获取城市中文名字列表, 展示使用
    public List<String> getProvincesNameList(Activity activity) {
        return getSomething(NAME_KEY, activity);
    }
    // 获取城市id列表, 查询天气接口使用
    public List<String> getProvincesIdList(Activity activity) {
        return getSomething(ID_KEY, activity);
    }

    @NotNull
    private List<String> getSomething(String Key,Activity activity) {
        List<String> provincesSthList = new ArrayList<>();

        try (SQLiteDatabase db = DatabaseHelper.getReadableDatabase()) {
            @SuppressLint("Recycle") Cursor cursor = db.query("PROVINCE",new String[]{Key},null,null,null,null,null);
            while(cursor.moveToNext()) {
                String nameText = cursor.getString(0);
                provincesSthList.add(nameText);
            }
        } catch (SQLiteException e) {
            Log.d("sqlite", e.getMessage());
            Toast toast = Toast.makeText(activity, "Database unavailable", Toast.LENGTH_SHORT);
            toast.show();
        }

        return provincesSthList;
    }

    public String getProvinceNameById(String id,Activity activity){
        return getSomeThingById(id, activity, NAME_KEY);
    }
    public int getRISKById(String id,Activity activity){
        return getSomeIntById(id, activity, RISK_KEY);
    }

    public int getProvincePhotoById(String id,Activity activity){
        return getSomeIntById(id, activity, IMAGE_RESOURCE_ID);
    }

    private int getSomeIntById(String id, Activity activity, String imageResourceId) {
        try (SQLiteDatabase db = DatabaseHelper.getReadableDatabase()) {
            @SuppressLint("Recycle") Cursor cursor = db.query("PROVINCE",new String[]{imageResourceId},"ID=?",new String[]{id},null,null,null);
            if(cursor.moveToFirst()) {
                return cursor.getInt(0);
            }
        } catch (SQLiteException e) {
            Log.d("sqlite", e.getMessage());
            Toast toast = Toast.makeText(activity, "Database unavailable", Toast.LENGTH_SHORT);
            toast.show();
        }
        return 0;
    }

    private String getSomeThingById(String id, Activity activity, String KEY) {
        try (SQLiteDatabase db = DatabaseHelper.getReadableDatabase()) {
            @SuppressLint("Recycle") Cursor cursor = db.query("PROVINCE",new String[]{KEY},"ID=?",new String[]{id},null,null,null);
            if(cursor.moveToFirst()) {
                return cursor.getString(0);
            }
        } catch (SQLiteException e) {
            Log.d("sqlite", e.getMessage());
            Toast toast = Toast.makeText(activity, "Database unavailable", Toast.LENGTH_SHORT);
            toast.show();
        }
        return "";
    }
    public void setRISKById(String id, Activity activity, int riskLevel){
        ContentValues riskValues = new ContentValues();
        riskValues.put("RISK",riskLevel);
        try (SQLiteDatabase db = DatabaseHelper.getWritableDatabase()){
            int row = db.update("PROVINCE",riskValues,"ID=?",new String[]{id});
            Log.d("sqlite","update row"+row);
        }catch (SQLiteException e){

            Log.e("sqlite",e.getMessage());
            Toast toast = Toast.makeText(activity,"Database unwritable",Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    public ArrayList<String> getProvinceNameByRiskLevel(String riskLevel, Activity activity){
        ArrayList<String> provincesList = new ArrayList<>();
        try (SQLiteDatabase db = DatabaseHelper.getReadableDatabase()) {
            @SuppressLint("Recycle") Cursor cursor = db.query("PROVINCE",new String[]{NAME_KEY},"RISK=?",new String[]{riskLevel},null,null,null);
            while(cursor.moveToNext()) {
                String nameText = cursor.getString(0);
                provincesList.add(nameText);
            }
        } catch (SQLiteException e) {
            Log.d("sqlite", e.getMessage());
            Toast toast = Toast.makeText(activity, "Database unavailable", Toast.LENGTH_SHORT);
            toast.show();
        }
        return provincesList;
    }
}
